package com.zh.springcloud.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zh.springcloud.common.Result;
import com.zh.springcloud.entity.Storage;
import com.zh.springcloud.mapper.StorageMapper;
import com.zh.springcloud.service.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @ClassName StorageServiceImpl
 * @date: 2021.08.12 18:44
 * @Author: zhanghang
 */
@Service
public class StorageServiceImpl implements StorageService {

	@Autowired
	private StorageMapper storageMapper;
	
	/**
	 * description: 扣减库存
	 * date: 2021年-08月-12日 18:45
	 * author: zhanghang
	 * 
	 * @param productId
 * @param count
	 * @return com.zh.springcloud.common.Result<?>
	 */ 
	@Override
	public Result<?> decrease(Long productId, Integer count) {
		// 根据产品id，查询产品
		Storage storage = storageMapper.selectOne(new QueryWrapper<Storage>().lambda().eq(Storage::getProductId, productId));
		storage.setUsed(storage.getUsed() +count);
		storage.setResidue(storage.getResidue() - count);
		storageMapper.updateById(storage);
		return Result.ok();
	}
}
